<?php

namespace app\admin\controller\health;

use app\common\controller\Backend;
use app\health\model\HealthActivityRecord;
use app\health\model\HealthCouponList;
use app\health\model\HealthOrder;
use app\health\model\HealthOrderGoods;
use app\health\model\HealthUser;
use app\health\model\HealthUserAccount;
use app\health\model\HealthUserAddress;
use app\health\model\HealthUserSignIn;
use app\health\model\HealthUserWithdraw;
use app\health\service\ConfigService;
use Exception;

class Config extends Backend
{
    /**
     * @var \app\health\model\HealthConfig
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\health\model\HealthConfig');
    }

    public function index()
    {
        $this->assign([
            'title' => '系统配置'
        ]);
        return $this->view->fetch();
    }

    /**
     * 设置平台配置参数
     * @Author 张旭鹏
     * @DateTime 2023-07-03
     */
    public function platform($type): string
    {
        if ($this->request->isPost()) {
            $data = $this->request->post("data");
            if ($data) {
                try {
                    $config = $this->model->get(['name' => $type]);
                    if (!$config) {
                        $this->model->allowField(true)->save([
                            'name'  => $type,
                            'title' => $this->request->post("title"),
                            'group' => $this->request->post("group"),
                            'type'  => 'array',
                            'value' => $data,
                        ]);
                    } else {
                        $config->value = $data;
                        $config->save();
                    }
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                ConfigService::getConfig(true);
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty'));
        }
        $config = $this->model->where(['name' => $type])->value('value');
        $config = json_decode($config, true);
        $this->assignconfig('row', $config);
        return $this->view->fetch();
    }

    public function clean_database()
    {
        $userId = $this->request->param('user_id');
        if ($userId) {
            $user = HealthUser::where('id', $userId)->find();
            if ($user) {
                $user->save(['balance' => 0, 'virtually_coin' => '0', 'sign_in_day' => 0, 'sign_in_day_total' => 0]);
                //清空签到数据
                HealthUserSignIn::where('user_id', $userId)->delete();
                //清空账户记录
                HealthUserAccount::where('user_id', $userId)->delete();
                //清空地址
                HealthUserAddress::where('user_id', $userId)->delete();
                //清空提现记录
                HealthUserWithdraw::where('user_id', $userId)->delete();
                //清空订单记录
                $orders  = HealthOrder::where('user_id', $userId)->select();
                $orderId = [];
                foreach ($orders as $order) {
                    $orderId[] = $order['id'];
                }
                HealthOrder::where('user_id', $userId)->delete();
                HealthOrderGoods::where('order_id', 'in', $orderId)->delete();
                //清空礼包数据
                HealthActivityRecord::where('user_id', $userId)->delete();
                //清空优惠券
                HealthCouponList::where('user_id', $userId)->delete();
                return 'SUCCESS';
            }
            return 'FAIL';
        }
        return 'FAIL';
    }
}
